package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //--1--发送验证码  请求url地址：/user/sendMsg
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //--生成验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //Integer code =1234;
            log.info("[>>>生成验证码-code]:{}",code);

            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //需要将生成的验证码保存到Session
            //session.setAttribute(phone,code);
            //需要将生成的验证码保存到Redis,设置过期时间
            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
            return R.success(code.toString());  //R.success("手机验证码短信发送成功")
        }
        return R.error("短信发送失败");
    }

    //--2--验证码登录  请求url地址：/user/login
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){

        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        log.info("[>>>登录信息]:phone={},code={}",phone,code);

        //String code_session= session.getAttribute(phone).toString();
        //从Redis中获取缓存的验证码
        Object code_session = redisTemplate.opsForValue().get(phone);
        //验证码判断
        if (code_session!=null && code_session.toString().equals(code)){
            //匹配成功
            User user = userService.getOne(
                    new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
            if (user==null){
                //新用户--自动注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user);
            log.info("[>>>session]:user={}",user);
            //从Redis中删除缓存的验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败");
    }
    //--T1-2：退出
    @PostMapping("/loginout")
    public R logout(HttpSession session){
        log.info("[>>>--<用户登录模块>用户退出]:----------------------");
        session.removeAttribute("user");
        log.info("[>>>--<用户登录模块>用户退出]:{}",session.getAttribute("user"));
        return R.success("退出成功");
    }


}
